<script lang="ts" setup>
import { ref } from 'vue'
import { handleMoveToBottom, isElementToBottom, debounce } from '@/utils/common.ts'
import { useChatGpt } from '@/hooks/useChatGpt'
// 弹窗组件
defineOptions({
  name: 'chat-dialog'
})
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  width: {
    type: String || Number,
    default: '500px'
  }
})
const emits = defineEmits(['update:visible'])

const { chatStore, userMessage, messageListRef, handleSendUserMessage, handleReceiveChunkMessage } =
  useChatGpt()

const handleToBottom = () => {
  if (messageListRef.value) {
    // 在0.5s以内滑到最底部
    handleMoveToBottom(messageListRef.value)
  }
}

const isShowBottomButton = ref<boolean>(false)

const handleScroll = debounce(() => {
  if (messageListRef.value) {
    isShowBottomButton.value = isElementToBottom(messageListRef.value)
  }
}, 200)

const handleCopyContent = (content: string) => {
  // console.log('点击了复制按钮。。。')
  navigator.clipboard.writeText(content)
  ElMessage.success('复制成功!')
}

const handlePardon = async () => {
  // 再次提问
  chatStore.setChatGptMessageLoading()
  chatStore.deleteFinalMessageResponse()
  await handleReceiveChunkMessage()
}
</script>

<template>
  <Transition>
    <section
      class="chat-dialog-mask"
      v-show="props.visible"
      @click.self="emits('update:visible', false)"
    >
      <div class="chat-dialog" :style="`width:${props.width}`">
        <!-- 左边聊天记录 -->
        <section class="chat-dialog__left">
          <section class="message-list scroll" ref="messageListRef" @scroll="handleScroll">
            <message-box
              v-for="(item, index) in chatStore.chatHistory"
              @copyContent="handleCopyContent"
              @pardon="handlePardon"
              :key="item.id"
              :text="item.msg"
              :name="item.name"
              :type="item.type"
              :isLoading="item.isLoading"
              :id="`flowChart${index}`"
              :isMicroFunctionRender="index !== 0 && index + 1 === chatStore.chatHistory.length"
              :isCouleBeReview="index !== 0"
              :chat_id="item.chat_id"
            ></message-box>
          </section>
          <chat-input v-model:modelValue="userMessage" @ok="handleSendUserMessage"></chat-input>
          <!-- 点击可返回最底部 -->
          <section
            class="chat-dialog__bottom-button"
            @click="handleToBottom"
            v-show="isShowBottomButton"
          >
            <el-icon><Bottom /></el-icon>
          </section>
        </section>
        <!-- 右侧放拟人卡通小猫 -->
        <section class="chat-dialog__right">
          <!-- 卡通形象的容器 -->
          <div class="cartoon_figure_container">
            <section class="avater-container">
              <img src="@/assets/images/codeMiao_avater.png" alt="" />
            </section>
          </div>
          <!-- 发送快捷讯息的按钮容器 -->
          <div class="function_button_container"></div>
        </section>
        <!-- 关闭按钮 -->
        <my-close-button @ok="emits('update:visible', false)"></my-close-button>
      </div>
    </section>
  </Transition>
</template>

<style lang="scss" scoped>
.chat-dialog-mask {
  position: fixed;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.5);
  .chat-dialog {
    position: relative;
    height: 80vh;
    background-color: #f2f2f2;
    // padding: 20px;
    border-radius: 10px;
    display: flex;
    overflow: hidden;
    .chat-dialog__left {
      position: relative;
      width: 80%;
      height: 100%;
      display: flex;
      flex-direction: column;
      padding: 10px;
      @media (max-width: 1000px) {
        width: 100%;
      }
      .message-list {
        height: 90%;
        overflow-y: auto;
      }
      .chat-dialog__bottom-button {
        position: absolute;
        display: flex;
        justify-content: center;
        align-items: center;
        bottom: 15%;
        left: 50%;
        transform: translateX(-50%);
        width: 27px;
        height: 27px;
        border-radius: 50%;
        background-color: #fff;
        cursor: pointer;
        transition: all 0.5s;
      }
    }
    .chat-dialog__right {
      flex: 1;
      height: 100%;
      background-color: #fff;
      display: flex;
      flex-direction: column;
      align-items: center;
      .cartoon_figure_container {
        display: flex;
        justify-content: center;
        align-items: center;
        max-width: 300px;
        max-height: 180px;
        border: 1px soild black;
        .avater-container {
          width: 100px;
          height: 100px;
          border-radius: 50%;
          overflow: hidden;
          background-color: #f2f2f2;
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
      .function_button_container {
        flex: 1;
        width: 100%;
        // background-color: pink;
      }
    }
  }
}
</style>
@/hooks/useDivDrag
